package com.ewanghuiju.app.ui.web;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.ewanghuiju.app.R;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;

/**
 * Created by stone on 2018/7/12.
 */

public class MyWebViewClient extends LinearLayout {
    X5WebView x5webView;
    ProgressView progressView;
    public MyWebViewClient(Context context) {
        super(context);
    }

    public MyWebViewClient(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.mywebviewclient,this,true);
        initView();
    }

    private static final String TAG = "MyWebViewClient";
    private void initView(){

        progressView = findViewById(R.id.progressView);
        progressView.setVisibility(GONE);
        //progressView.setColor();
        x5webView = findViewById(R.id.x5webView);

        x5webView.setIsOfficeListener(new X5WebView.IsOfficeListener() {
            @Override
            public void isOffice(String url,boolean isDown) {
                isOffices(url,isDown);
            }
        });
        x5webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView webView, int i) {
                super.onProgressChanged(webView, i);
                setProgress(i);

            }
        });
    }
    public void setProgress(int i){
        progressView.setProgress(i);
        if (i==0){
            progressView.setVisibility(GONE);
        }else if (i>0&&i<100){
            progressView.setVisibility(VISIBLE);

        }else {
            progressView.setVisibility(GONE);
        }
    }


    public void isOffices(String url,boolean isDown){
        x5webView.downloadByBrowser2(url,isDown);
    }
    public void loadUrl(String url){
        x5webView.loadUrl(url);
    }
    public X5WebView getWebView(){
        return x5webView;
    }
    public void setOnPageListener(X5WebView.OnPageListener listener){
        x5webView.setOnPageListener(listener);
    }


}
